var urlArgs = Suningmatch.getUrlArgs();

var vm = new Vue({
    el: '#home',
    data: {
        user: {},
        isanonymous: false,
        nmObj: {},
        criticType: 1,
        commContent: "",
        uploadPop: false,
        isWX: false,
        pointPop: false

    },
    computed: {
        surplusNum: function() {

        }
    },
    methods: {
        oaOvertime: function(){
            //OA超时
            vm.pointPop = true;
            vm.pointTip = "登录超时，请重新刷新页面";
            setTimeout(function() {
                vm.pointPop = false;
                vm.pointTip = "";
            }, 2000);
        },
        whitecomm: function() {
            if(vm.isanonymous) {
                vm.criticType = 3;
            } else {
                var ua = window.navigator.userAgent.toLowerCase();
                if(ua.match(/MicroMessenger/i) == 'micromessenger') {
                    vm.criticType = 2;
                }else{
                    vm.criticType = 1;
                }
            }
            var dataUrl = Suningmatch.DI.whiteComm,
                dataObj = {
                    "topicId": urlArgs.topicId,
                    "criticType": vm.criticType,
                    "nmHeadPic": vm.isanonymous ? vm.nmObj.headPic : null,
                    "nmName": vm.isanonymous ? vm.nmObj.name : null,
                    "commContent": vm.commContent
                };
            if(vm.isWX){
                dataUrl = Suningmatch.DI.WXwhiteComm,
                dataObj = {
                    "topicId": urlArgs.topicId,
                    "criticType": vm.criticType,
                    "nmHeadPic": vm.isanonymous ? vm.nmObj.headPic : null,
                    "nmName": vm.isanonymous ? vm.nmObj.name : null,
                    "commContent": vm.commContent,
                    "openId": urlArgs.wx_openid
                };
            }

            $.ajax({
                type: "post",
                url: dataUrl,
                data: dataObj,
                dataType: "json",
                success: function(response) {
                    if(response!=null && response.idsIntercepted==true){
                        vm.oaOvertime();
                        return;
                    }
                    if(response.retFlag == 1) {
                        location.href = "topic_index.html?topicId=" + urlArgs.topicId;
                    }
                },
                error: function(err) {
                    console.log(err);
                }
            });
        },
        backPage: function() {
            location.href = "topic_index.html?topicId=" + urlArgs.topicId;
        },
        //点击匿名
        anonymousWhite: function() {
            vm.isanonymous = !vm.isanonymous;
            if(!vm.isanonymous) {
                if(vm.isWX) {
                    vm.criticType = 2;
                    this.nmObj = {
                        name: urlArgs.wx_nickname,
                        headPic: urlArgs.wx_headimgurl
                    }
                } else {
                    vm.criticType = 1;
                    
                    this.nmObj = {
                        name: vm.user.nickname,
                        headPic: vm.user.nickimg
                    }
                }
                return;
            }
            vm.criticType = 3;
            $.ajax({
                type: "post",
                url: Suningmatch.DI.getNmInfo,
                dataType: "json",
                success: function(response) {
                    vm.nmObj = response.data;
                },
                error: function(err) {
                    console.log(err);
                }
            });
        },
        changePic: function() {
            vm.criticType = 3;
            $.ajax({
                type: "post",
                url: Suningmatch.DI.getNmInfo,
                dataType: "json",
                success: function(response) {
                    vm.nmObj = response.data;
                },
                error: function(err) {
                    console.log(err);
                }
            });
        },
        replaceImg: function() {
            setTimeout(function() {
                vm.uploadPop = true;
            }, 1000);

            cropperImg();
        },
        cancle: function() {
            vm.uploadPop = false;
        },
        uploadImg: function() {
            //调用上传图片接口
            var x = $("#preview-x").val(),
                y = $("#preview-y").val(),
                width = $("#preview-width").val(),
                height = $("#preview-height").val();

            var canvas = $('<canvas style="display:none;" width="' + width + '" height="' + height + '"></canvas>')[0],
                ctx = canvas.getContext('2d');
            ctx.drawImage(Suningmatch.cropper, x, y, width, height, 0, 0, width, height);
            var data = canvas.toDataURL();
            // dataURL 的格式为 “data:image/png;base64,****”,逗号之前都是一些说明性的文字，我们只需要逗号之后的就行了
            data = data.split(',')[1];
            data = window.atob(data);
            var ia = new Uint8Array(data.length);
            for(var i = 0; i < data.length; i++) {
                ia[i] = data.charCodeAt(i);
            };
            // canvas.toDataURL 返回的默认格式就是 image/png
            var blob = new Blob([ia], {
                type: "image/png"
            });
            var fd = new FormData();
            fd.append('image', blob);
            $.ajax({
                type: "post",
                url: Suningmatch.DI.uploadMisc,
                dataType: "json",
                data: fd,
                contentType: false, //不可缺
                processData: false, //不可缺
                success: function(response) {
                    if(response.retFlag == 1) {
                    	var hpic = ""
                    	if (location.host.indexOf("dj.suning.com") != -1) {
                    		hpic= "http://image?.suning.cn" + response.data;
                    	} else {
                    		hpic= "http://uimgpre.cnsuning.com" + response.data;
                    	}
                        hpic = Suningmatch.imgprefix(hpic);
                        vm.uploadPop = false;
                        vm.nmObj.headPic = hpic;

                        $.ajax({
                            type: "post",
                            url: Suningmatch.DI.modifyUserHeadPic,
                            dataType: "json",
                            data: {
                                "headPic": hpic
                            },
                            success: function(response) {
                                console.log("上传成功！" + response)
                            },
                            error: function(err) {
                                console.log(err);
                            }
                        });
                    }
                },
                error: function(err) {
                    console.log(err);
                }
            });
        }
    },
    ready: function() {
        var ua = window.navigator.userAgent.toLowerCase();
        if(ua.match(/MicroMessenger/i) == 'micromessenger') {
            this.isWX = true;
            this.criticType = 2;
            if(location.href.indexOf("wx_openid") == -1) {
                location.href = Suningmatch.wxLogin + encodeURIComponent(location.href);
            }
            this.user = {
                nickname: urlArgs.wx_nickname,
                nickimg: urlArgs.wx_headimgurl
            }
            this.nmObj = {
                name: urlArgs.wx_nickname,
                headPic: urlArgs.wx_headimgurl
            }
        } else {
            this.isWX = false;
            this.criticType = 1;
            if(location.href.indexOf("isLogin") == -1) {
                location.href = Suningmatch.DI.soaTopic + "?redirectUrl=" + location.href;
            }
            $.ajax({
                type: "post",
                url: Suningmatch.DI.getLoginInfo,
                dataType: "json",
                success: function(response) {
                    if(response!=null && response.idsIntercepted==true){
                        location.href = Suningmatch.DI.soaTopic + "?redirectUrl=" + location.href;
                    }
                    
                    var userData = response.data;
                    vm.user = {
                        nickname: userData.personName,
                        nickimg: userData.headPic
                    }
                    vm.nmObj = {
                        name: userData.personName,
                        headPic: userData.headPic
                    }
                },
                error: function(err) {
                    console.log(err);
                }
            });
        }

    }
});